function cutString(str, len) {
    //length属性读出来的汉字长度为1
    if (str.length * 2 <= len) {
        return str;
    }
    var strlen = 0;
    var s = "";
    for (var i = 0; i < str.length; i++) {
        s = s + str.charAt(i);
        if (str.charCodeAt(i) > 128) {
            strlen = strlen + 2;
            if (strlen >= len) {
                return s.substring(0, s.length - 1) + "...";
            }
        } else {
            strlen = strlen + 1;
            if (strlen >= len) {
                return s.substring(0, s.length - 2) + "...";
            }
        }
    }
    return s;
}

function get_comment(movie_id) {
    console.log(1);
    $.get('/movie/movie_comment_parise/', {
            'movie_id': movie_id,
        }, function (data) {
            var obj = JSON.parse(data);
            console.log(obj);
            var comment_all = obj['comment_info'];
            var comment_num = Object.keys(comment_all).length;
            if (comment_all !== '') {
                for (i = 0; i < comment_num; i++) {
                    $(".tent_info").append('<div class="one cont">' +
                        '<div class="cont_img">' +
                        '<img src="' + comment_all[i].avatar + '">' +
                        '</div>' +
                        '<div class="onecontent">' +
                        '<div class="comment-right">' +
                        '<h4>' + comment_all[i].name + '</h4>' +
                        '<div class="comment-content-header three">' +
                        '<span><i class="glyphicon glyphicon-time two"></i>' + '发布于' + comment_all[i].time + '</span>&nbsp;&nbsp;&nbsp;&nbsp;' +
                        '</div>' +
                        '<div class="ms-cont1" style="cursor: pointer;" onclick="$(this).hide().next().show()">' + cutString(comment_all[i].content, 255) + '</div>' +
                        '<div class="ms-cont2" style="display: none;cursor: pointer;" onclick="$(this).hide().prev().show()">' + comment_all[i].content + '</div>' +
                        '<br>' +
                        '<div class="comment-content-footer">' +
                        '</div>' +
                        '<div class="reply-list three">' +
                        '</div>' +
                        '<div class="reply_active" style="cursor: pointer;" onclick="$(this).next().toggle()">' +
                        '<img src="/static/images/reply_icon.png" width="30px" height="20px" style="float: right"></div>' +
                        '<div class="one three" style="display: none">' +
                        '<textarea class="con_back" id="con_back"></textarea>' +
                        '<div class="con_btn btn_back" id="' + comment_all[i].comment_id + '">回复</div>' +
                        '</div>' +
                        '</div>' +
                        '</div>');
                    var reply_comment_id = comment_all[i].comment_id;
                    $.get('/movie/movie_comment_reply/', {
                        'comment_id': reply_comment_id
                    }, function (data) {
                        var robj = JSON.parse(data);
                        var reply_all = robj['reply_info'];
                        var reply_num = Object.keys(reply_all).length;
                        if (reply_all !== '') {
                            for (j = 0; j < reply_num; j++) {
                                var html_back;
                                var reply_s = '#' + reply_all[j].reply_id;
                                html_back = '<div class="reply">' +
                                    '<div class="rp_three">' +
                                    '<a class="replyname">' + reply_all[j].name + '</a>:<span >回复于</span><span>' + reply_all[j].time + '</span>' +
                                    '</div>' +
                                    '</div>' +
                                    '<p><span>' + reply_all[j].content + '</span></p>';
                                $(reply_s).parent().parent().parent().find(".reply-list").append(html_back);
                            }
                        }
                    })
                }
            } else {
                console.log('还没有人评论');
            }
        }
    );
};
$(".btn_send").on('click', function () {
    var text_send = $("#text_send").val();
    if (text_send == "") {
        return false;
    }
    $.get('/user/user_new_comment/', {
        'content': text_send,
        'media_id': 1
    }, function (data) {
        data = JSON.parse(data);
        if (data['status'] == 200) {
            var now = time();
            var name = $('#user_name').text();
            var avator_info = $('.hr_avator').css('backgroundImage');
            var avator = avator_info.split('(')[1].split(')')[0];
            var html;
            html =
                '<div class="one cont">' +
                '<div class="cont_img">' +
                '<img src=' + avator + '>' +
                '</div>' +
                '<div class="onecontent">' +
                '<div class="comment-right">' +
                '<h4>' + name + '</h4>' +
                '<div class="comment-content-header three">' +
                '<span><i class="glyphicon glyphicon-time two"></i>' + '发布于' + now + '</span>&nbsp;&nbsp;&nbsp;&nbsp;' +
                '</div>' +
                '<div class="ms-cont1" style="cursor: pointer;" onclick="$(this).hide().next().show()">' + cutString(text_send, 255) + '</div>' +
                '<div class="ms-cont2" style="display: none;cursor: pointer;" onclick="$(this).hide().prev().show()">' + text_send + '</div>' +
                '<br>' +
                '<div class="comment-content-footer">' +
                '</div>' +
                '<div class="reply-list three">' +
                '</div>' +
                '<div class="reply_active" style="cursor: pointer;" onclick="$(this).next().toggle()">' +
                '<img src="/static/images/reply_icon.png" width="30px" height="20px" style="float: right"></div>' +
                '<div class="one three" style="display: none">' +
                '<textarea class="con_back" id="con_back"></textarea>' +
                '<div class="con_btn btn_back">回复</div>' +
                '</div>' +
                '</div>' +
                '</div>';
            $(".tent_info").append(html);
            $(".text_send").val("");
        } else if (data['text'] == "请登录后评论") {
            alert("请登录后评论");
        } else {
            alert("评论失败，请刷新后重试")
        }

    });
});

$(".content_text").show();
$(".tent_info").on('click', '.btn_back', function () {
    var reply_id = $(this).attr('id');
    var reply_s = '#' + reply_id;
    var con_back = $(this).parent().parent().parent().find("#con_back").val();
    if (con_back == "") {
        return false;
    }
    $.get('/user/user_new_reply/', {
        'media_id': 1,
        'reply_id': reply_id,
        'content': con_back,
    }, function (data) {
        console.log(data);
        data = JSON.parse(data);
        if (data['status'] == 200) {
            var name1 = $('#user_name').text();
            var back_time = time();
            var html_back;
            html_back = '<div class="reply">' +
                '<div class="rp_three">' +
                '<a class="replyname">' + name1 + '</a>:<span >回复于</span><span>' + back_time + '</span>' +
                '</div>' +
                '</div>' +
                '<p><span>' + con_back + '</span></p>';
            $(reply_s).parent().parent().parent().find(".reply-list").append(html_back);
            $(reply_s).parent().parent().parent().find("#con_back").val("");
        } else if (data['text'] == "请登录后回复") {
            alert("请登录后回复");
        } else {
            alert("回复失败，请刷新后重试")
        }
    });
});

function time() {
    function time(s) {
        return s < 10 ? '0' + s : s;
    }

    var myDate = new Date();
    var year = myDate.getFullYear();
    var month = myDate.getMonth() + 1;
    var date = myDate.getDate();
    var h = myDate.getHours();
    var m = myDate.getMinutes();
    var s = myDate.getSeconds();
    return year + '年' + time(month) + "月" + time(date) + "日" + " " + time(h) + ':' + time(m) + ":" + time(s);
}
